<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

</head>
<body>
<h1>修改员工信息</h1>
<form id="addform">
  员工id<input id="empId" name="empId" type="text"  readonly><br>
  员工姓名<input id="empName" name="empName" type="text"  ><br>
  员工性别<input id="genderMan" name="gender" type="radio" value="1">男
         <input id="genderWomen" name="gender" type="radio" value="0" >女

  <br>
  员工生日<input id="birthday" name="birthday" type="date" ><br>
  入职日期<input id="hireDate" name="hireDate" type="date"> <br>
  工资<input id="salary" name="salary" type="text" value="${requestScope.editEmp.salary}"><br>
  住址<input id="address" name="address" type="text" value="${requestScope.editEmp.address}"><br>
  部门信息<select id="empDeptId" name="empDeptId">

</select>
  <input id="button" type="button" onclick="addemp()" value="提交">
</form>
</body>

<script>
  $(document).ready(function () {
      loadDept();
  })

  function getEmp() {
    var empId = location.href.substr(location.href.indexOf("=")+1)
    $.ajax({
      type: "get",
      dataType: "json",
      url: "/admin/emp/toeditjson/"+empId,
      success: function (result) {
        if (result.errorCode != 500){
          var emp = result.data;
          $("#empId").val(emp.empId);
          $("#empName").val(emp.empName);
          if (emp.gender==1){
              $("input[id='genderMan']").prop("checked",true)
          }else {
              $("input[id='genderWomen']").prop("checked",true)
          }

          $("#birthday").val(emp.birthday);
          $("#hireDate").val(emp.hireDate);
          $("#salary").val(emp.salary);
          $("#address").val(emp.address);
          $("#empDeptId").val(emp.department.deptId);
        }else {
          alert(result.errorMsg)
        }

      }
    })


  }


  function loadDept() {
    $.ajax({
      type: "get",
      dataType: "json",
      url: "/admin/emp/getdepts",
      success:function (result) {
        $("select").html("");
        var alldept = result.data;
        $("select").append(`<option value="${alldept[0].deptId}">${alldept[0].deptName}</option>`)
        for ( i = 1; i < alldept.length; i++) {
          var dept = alldept[i];
          $("select").append(`
          <option value="${dept.deptId}">${dept.deptName}</option>
          `)
        }
        getEmp()
      }
    });
  }


  function addemp() {
    $.ajax({
      type: "get",
      dataType: "json",
      url: "/admin/emp/doeditjson",
      data: $("#addform").serialize(),
      success: function (result) {
        if (result.data > 0 ){
          window.location.href="/pages/emp_list.html";
        }else {
          alert(result.errorMsg)
        }
      }
    })
  }


</script>

</html>